package ac.zlf.springmybatis.service.impl;

import ac.zlf.springmybatis.base.entity.Amenu;
import ac.zlf.springmybatis.base.entity.User;
import ac.zlf.springmybatis.base.util.DateUtils;
import ac.zlf.springmybatis.constant.AppExceptionStatus;
import ac.zlf.springmybatis.constant.Column;
import ac.zlf.springmybatis.constant.Indicator;
import ac.zlf.springmybatis.core.exception.AppException;
import ac.zlf.springmybatis.domain.AmenuBean;
import ac.zlf.springmybatis.mapper.AmenuBeanMapper;
import ac.zlf.springmybatis.service.AmenuService;
import ac.zlf.springmybatis.core.mybatis.domain.Page;
import ac.zlf.springmybatis.core.mybatis.domain.PageRequest;
import ac.zlf.springmybatis.util.CommonValidate;
import ac.zlf.springmybatis.vo.MenuVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>Project: bee-projects</p>
 * <p>Title: AmenuServiceImpl.java</p>
 * <p>Description:   </p>
 * <p>Copyright: Copyright (c) 2016 </p>
 * <p>Company: 华炜云商科技有限公司 www.hwtech.cc</p>
 *
 * @author zh
 * @version 1.0
 * @date 2017-03-09
 */
@Service
public class AmenuServiceImpl implements AmenuService {

    @Autowired
    private AmenuBeanMapper amenuBeanMapper;

    @Override
    public AmenuBean findBeanById(Integer id, Integer aId) {
        return amenuBeanMapper.selectBeanById(id, aId);
    }

    @Override
    public AmenuBean findAvailableBeanById(Integer id, Integer aId) {
        AmenuBean amenuBean = null;

        if (CommonValidate.isEmpty(id)) {
            throw new AppException(AppExceptionStatus.DATA_NONE_OR_STATUS_ERROR);
        }
        amenuBean = this.findBeanById(id, aId);

        if (amenuBean == null) {
            throw new AppException(AppExceptionStatus.DATA_NONE_OR_STATUS_ERROR);
        }

        return amenuBean;
    }

    @Override
    public Page<AmenuBean> findBeanPage(PageRequest pageRequest, AmenuBean amenuBean, Integer aId) {
        return amenuBeanMapper.selectBeanPage(pageRequest, amenuBean, aId);
    }

    @Override
    public List<AmenuBean> findBeanAll(AmenuBean amenuBean, Integer aId) {
        return amenuBeanMapper.selectBeanAll(amenuBean, aId);
    }

    @Override
    @Transactional
    public void doAdd(AmenuBean amenuBean, User user) {
        this.doValidateForUpOrAdd(amenuBean, true);
        Amenu amenuSql = new Amenu();
        this.doCopyFromPageToSql(amenuBean, amenuSql);
        amenuSql.setParentId(amenuBean.getParentId());
        amenuSql.setGarde(amenuBean.getGarde());
        amenuSql.setCrateDt(DateUtils.getCurrentDate());
        amenuSql.setStatus(Indicator.STATUS_1.getCode());
        if (!CommonValidate.isEmpty(amenuSql.getParentId())) {
            AmenuBean amenuBeanParent = amenuBeanMapper.selectBeanById(amenuSql.getParentId(), user.getId());
            if(Indicator.MENU_GRADE_3.isCode(amenuBeanParent.getGarde())){
                throw new AppException(AppExceptionStatus.DATA_NONE);
            }
            amenuSql.setGarde(amenuBeanParent.getGarde()+ 1);
        }else {
            amenuSql.setGarde(Indicator.MENU_GRADE_1.getCode());
        }
        //保存到数据库
        amenuBeanMapper.insert(amenuSql);
    }

    @Override
    @Transactional
    public void doUpdate(AmenuBean amenuBean, User user) {
        this.doValidateForUpOrAdd(amenuBean, false);
        Amenu amenuSql = this.findBeanById(amenuBean.getId(), null);

        if (amenuSql == null) {
            throw new AppException(AppExceptionStatus.DATA_NONE);
        }

        this.doCopyFromPageToSql(amenuBean, amenuSql);
        amenuSql.setUpdateDt(DateUtils.getCurrentDate());
        //保存到数据库
        amenuBeanMapper.updateByPrimaryKey(amenuSql);
    }

    @Override
    @Transactional
    public void doDel(Integer id, User user) {
        Amenu amenu = amenuBeanMapper.selectByPrimaryKey(id);
        if (CommonValidate.isEmpty(amenu)) {
            throw new AppException(AppExceptionStatus.DATA_NONE);
        }
        //查询是否有子节点
        List<AmenuBean> amenuBeanList = amenuBeanMapper.selectBeanByParentId((AmenuBean) amenu, user.getId());
        if (!amenuBeanList.isEmpty()) {
            throw new AppException(AppExceptionStatus.DATA_NONE);
        }
        amenu.setStatus(Indicator.STATUS_2.getCode());
        amenuBeanMapper.updateByPrimaryKey(amenu);
    }

    @Override
    @Transactional
    public void doDelete(Integer id, User user) {
//        amenuBeanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public MenuVo getMenu(User user) {
        List<AmenuBean> amenuBeanList = amenuBeanMapper.selectBeanAll(new AmenuBean(), user.getId());
        MenuVo menuVo = new MenuVo();
        for (AmenuBean amenuBean : amenuBeanList) {
            Integer grade = amenuBean.getGarde();
            if (Column.MENU_GRADE.isContainCode(grade)) {
                if (Indicator.MENU_GRADE_1.getCode().equals(grade)) {
                    menuVo.getAmenuBeanSet1().add(amenuBean);
                } else if (Indicator.MENU_GRADE_2.isCode(grade)) {
                    menuVo.getAmenuBeanSet2().add(amenuBean);
                } else {
                    menuVo.getAmenuBeanSet3().add(amenuBean);
                }
            }
        }
        return menuVo;
    }

    /**
     * 将页面传来的数据复制到待保存的对象中
     *
     * @param amenuBeanPage [必填] 页面传来的对象(跟页面相关)
     * @param amenuSql      [必填] 待保存的对象(跟数据库相关)
     */
    private void doCopyFromPageToSql(AmenuBean amenuBeanPage, Amenu amenuSql) {
        amenuSql.setFacion(amenuBeanPage.getFacion());
        amenuSql.setMenuName(amenuBeanPage.getMenuName());
        amenuSql.setUrl(amenuBeanPage.getUrl());
        amenuSql.setSort(amenuBeanPage.getSort());
    }

    /**
     * 验证页面传来的对象
     *
     * @param amenuBeanPage [必填] 页面传来的对象(跟页面相关)
     * @param isAdd         [必填] 是否是添加。true=是，当添加验证；false=否，当修改验证
     */
    private void doValidateForUpOrAdd(AmenuBean amenuBeanPage, boolean isAdd) {

    }
}